import { _decorator, Color, Component, Label, Node, Sprite } from 'cc';
import { GameData } from '../system/GameData';
import { DataHelper } from '../system/DataHelper';
import { buildingPlatform_config, PopView } from '../manager/AppControll';
import { SoundManager } from '../manager/SoundManger';
import { PopManager } from '../manager/PopManager';
import { SystemMessage } from './SystemMessage';
const { ccclass, property } = _decorator;

@ccclass('BuildingPlatform')
export class BuildingPlatform extends Component {
    @property(Label)
    mlbConsume: Label = null;

    private mNdUpdate: Node = null;
    private mNdMaxLevel: Node = null;
    private mConfig: buildingPlatform_config = null;
    private mNdBuildLv: Node = null;
    private mNdRandomGunLv: Node = null;
    private mNMaxKey: number = null;
    private mNMaxLv: number = null;
    private mNlv: number = null;
    private mNdUpdateUse: Node = null;
    private mNdCondition: Node = null;
    private mNdUpgradeBtn: Node = null;

    protected onLoad(): void {
        this.mNdMaxLevel = this.node.getChildByName("maxLevel");
        this.mNdUpdate = this.node.getChildByName("bulidingUpgrade");
        this.mNdBuildLv = this.node.getChildByName("bulidingLevel");
        this.mNdRandomGunLv = this.node.getChildByName("randomGunLevel");
        this.mNdUpdateUse = this.mNdUpdate.getChildByName("upgradeUse");
        this.mNdCondition = this.mNdUpdate.getChildByName("condition");
        this.mNdUpgradeBtn = this.mNdUpdate.getChildByName("upgradeBtn")
    }

    InitBuildingPlatform() {
        this.mConfig = DataHelper.GetBuildingPlatformConfigByLv(GameData.playerData.craftingTableLv);
        let maxKey = -Infinity;
        for (const key in DataHelper.GetBuildingPlatformConfig()) {
            const numericKey = parseInt(key, 10);
            if (numericKey > maxKey) {
                this.mNMaxKey = numericKey;
            }
        }
        this.mNMaxLv = DataHelper.GetBuildingPlatformConfigById(this.mNMaxKey).buildLevel;

        if (GameData.playerData.craftingTableLv == this.mNMaxLv) {
            this.mNdUpdate.active = false;
            this.mNdMaxLevel.active = true
        } else {
            this.mNdUpdate.active = true;
            this.mNdMaxLevel.active = false;

        }
        this.InitView();
    }

    InitView() {
        if (!this.mConfig) {
            console.error("BuildingPlatform Config不存在")
            return;
        }
        this.mNdBuildLv.getComponent(Label).string = this.mConfig.buildLevel + "";
        this.mNdRandomGunLv.getComponent(Label).string = this.mConfig.randomLevelValue;
        if (this.mConfig.buildLevel < this.mNMaxLv) {
            let consume = DataHelper.GetBuildingPlatformConfigByLv(this.mConfig.buildLevel + 1).upgradeConsume;
            this.mlbConsume.string = consume + "";
            if (GameData.playerData.Gold < consume) {
                this.mlbConsume.color = Color.RED;
                this.mNdUpgradeBtn.getChildByName("yellowPoint").active = false;
            } else {
                this.mlbConsume.color = Color.WHITE;
                this.mNdUpgradeBtn.getChildByName("yellowPoint").active = true;
            }
        }

        if (this.mConfig.promotion != null && GameData.playerData.Honor < this.mConfig.promotion) {
            this.mNdCondition.active = true;
            this.mNdUpgradeBtn.getComponent(Sprite).grayscale = true;
            this.mNdUpdateUse.active = false;
            this.mNdCondition.getComponent(Label).string = "荣誉值达到" + this.mConfig.promotion + "可升级";
            this.mNdUpgradeBtn.getChildByName("yellowPoint").active = false
        } else {
            this.mNdUpgradeBtn.getComponent(Sprite).grayscale = false;
            this.mNdUpdateUse.active = true;;
            this.mNdCondition.active = false;
        }
    }

    OnBtnComfirm() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (this.mConfig.promotion != null && GameData.playerData.Honor < this.mConfig.promotion) {
            PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, (node) => {
                node.getComponent(SystemMessage).InitContent("未达成升级条件");
            })
        } else {
            if (GameData.playerData.Gold > DataHelper.GetBuildingPlatformConfigByLv(this.mConfig.buildLevel + 1).upgradeConsume) {
                GameData.AddGold(DataHelper.GetBuildingPlatformConfigByLv(this.mConfig.buildLevel + 1).upgradeConsume * -1);
                GameData.playerData.craftingTableLv++;
                GameData.SavePlayData();
                this.InitBuildingPlatform()
            } else {
                PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, (node) => {
                    node.getComponent(SystemMessage).InitContent("金币不足");
                })
            }
        }

    }


}


